/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ufcg.dsc.lsi.vademecum.context.provider;

import br.edu.ufcg.dsc.lsi.vademecum.context.RawData;
import java.util.Vector;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

/**
 *
 * @author Hugo
 */
public class GPSRawDataProvider extends AbstractRawDataProvider implements LocationListener {

    private static GPSRawDataProvider instance = null;

    public GPSRawDataProvider() {
        RawDataListener listener = new LocationContextProvider();
        addRawDataListener(listener);
    }

    public static GPSRawDataProvider getInstance() {
        if (instance == null) {
            instance = new GPSRawDataProvider();
        }
        return instance;
    }

    public RawData createRawData(Location location) {
        RawData rawData = new RawData();
        rawData.setSensedData(location);
        return rawData;
    }

    public void locationUpdated(LocationProvider provider, Location location) {
        Vector rawDataListeners = getRawDataListeners();
        if (rawDataListeners == null) {
            return;
        }
        RawData rawData = createRawData(location);
        System.out.println("Localização atualizada - " +location);
        for (int index = 0; index < rawDataListeners.size(); index++) {
            RawDataListener listener = (RawDataListener) rawDataListeners.elementAt(index);
            listener.sensedDataUpdated(this, rawData);
        }
    }

    public void providerStateChanged(LocationProvider arg0, int arg1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void run() {
        try {
            LocationProvider locationProvider = LocationProvider.getInstance(null);
            locationProvider.setLocationListener(this, 10, -1, -1);
        // write post-initialize user code here
        } catch (LocationException ex) {
            ex.printStackTrace();
        }
    }
}
